package com.imonsoft.sqm.web.business.supplier_quality;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.business.supplier_quality.EnvManage;
import com.imonsoft.sqm.service.business.supplier_quality.EnvManageService;
import com.imonsoft.sqm.vo.business.supplier_quality.EnvManageVo;

@Controller
@RequestMapping("/business/supplier_quality/env_manage")
public class EnvManageControl {

	protected final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private EnvManageService envManageService;
	
	@RequestMapping("/listUI.do")
	public void listUI() {
	}
	
	
	@RequestMapping(value = "/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, EnvManage envManage) {
		DataGrid<EnvManageVo> dataGrid = new DataGrid<EnvManageVo>();
		try {
			 
			dataGrid = envManageService.getDataGridVoBy(envManage, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}
	
	@RequestMapping(value = "/addUI.do")
	public void addUI(ModelMap model) {

	}
	
	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response,@RequestParam("ids") List<Long> ids) {
		ReturnMessage message = new ReturnMessage();
		try {
			envManageService.deleteEnvManage(ids);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	
	
	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, EnvManage bean,BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			String envManage = request.getParameter("envManage"); // 获取总目
			envManageService.txAddEnvManage(envManage,message,result);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
	

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id) {

		if(id==null){
			new Exception(CCommon.EXCEPTION_MSG);
		} 
		try {
			EnvManageVo vo = envManageService.getModifyData(id);
			model.put("envManageVo", vo); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	
	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response) {
		ReturnMessage message = new ReturnMessage();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		try {
			
			String envManage = request.getParameter("envManage"); // 获取细目
			envManageService.txUpdateOrAddEnvManage(envManage);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
}
